#ifndef __QUMI_CONFIG_H
#define __QUMI_CONFIG_H

#include "stm8s.h"

//#define ENABLE_IWDG   (1)

//Scheduler TIM4. 16MHz / 128 / 250 = 500Hz(2ms)
#define TIM4_PRESCALER (TIM4_PRESCALER_128)
#define TIM4_CCR_Val   (250)

#define SCHED_CNT_SECOND (500L)
#define SCHED_CNT_MINUTE (SCHED_CNT_SECOND * 60L)
#define SCHED_CNT_HOUR   (SCHED_CNT_MINUTE * 60L)
#define SCHED_CNT_DAY    (SCHED_CNT_HOUR * 24L)

//TIM2 10Hz
#define TIM2_TOTAL_LEN (6250)
#define TIM2_CCR3_Val  (3120)

//TIM1 1KHz
#define TIM1_PRESCALER   (16)
#define TIM1_PERIOD      (1000)
//CH1 CH1N humidity power
#define TIM1_CH1_PERIOD  (500)
//CH2 FAN power
#define TIM1_FAN_PERIOD  (500)

//#define LED_WATER_PORT (GPIOD)
//#define LED_WATER_BLUE (GPIO_PIN_1)
//#define LED_WATER_PINS (LED_WATER_BLUE)

//power led
#define LED_POWER_PORT (GPIOA)
#define LED_POWER_PIN  (GPIO_PIN_3)

//battery adc
#define BATTERY_CH      (ADC1_CHANNEL_4)

//warning level:1.4V. ADC value = 1.4/vdd * 1024 = 1.4/3.3 * 1024 = 434
#define BATTERY_THRESHOLD (434)

//thermistor pin
#define THERMISTOR_PORT (GPIOD)
#define THERMISTOR_PIN  (GPIO_PIN_2)
#define R               (10000)
#define THERMISTOR_CH   (ADC1_CHANNEL_3)

//nrf24l01
#define SPI_CE_PORT         (GPIOA)
#define SPI_CE_PIN          (GPIO_PIN_1)
#define SPI_IRQ_PORT        (GPIOA)
#define SPI_IRQ_PIN         (GPIO_PIN_2)

//humidity pin
#define HUMIDITY_PORT   (GPIOC)
#define HUMIDITY_PIN    (GPIO_PIN_4)
#define R_HUMI          (10)  //1K unit
#define HUMIDITY_CH     (ADC1_CHANNEL_2)

//soft SPI
#define SPI_SCK_PORT          (GPIOD)
#define SPI_SCK_PIN           (GPIO_PIN_4)
#define SPI_CS_PORT           (GPIOB)
#define SPI_CS_PIN            (GPIO_PIN_4)
#define SPI_MOSI_PORT         (GPIOB)
#define SPI_MOSI_PIN          (GPIO_PIN_5)
#define SPI_MISO_PORT         (GPIOC)
#define SPI_MISO_PIN          (GPIO_PIN_5)


//GPIOA PINS output PA1->nrf2101 CE PA2->nrf24l01 IRQ
#define GPIOA_OUTPUT_PINS            (GPIO_PIN_1 | GPIO_PIN_2)
//GPIOA INPUT PINS : PA1->Key PA3->Water shortage
//#define GPIOA_INPUT_PINS             (GPIO_PIN_1 | GPIO_PIN_3)
//GPIOB OUTPUT PINS: PB4->SPI_CS PB5->SPI_MOSI
//#define GPIOB_OUTPUT_PINS            (GPIO_PIN_4 | GPIO_PIN_5)

//GPIOC INPUT PINS : PC4->AIN2(humidity) PC5->SPI_MISO
#define GPIOC_INPUT_PINS             (GPIO_PIN_4 | GPIO_PIN_5)

//GPIOD OUTPUT PINS : PD4->SPI_SCK PD1->Water led
#define GPIOD_OUTPUT_PINS            (GPIO_PIN_4 | GPIO_PIN_1)


#define WIFI_TIMEOUT_CNT    (6)

struct sensor_data_s
{
  int8_t  temp;
  uint8_t humi;
};

struct qumi_sensor_s
{
  struct sensor_data_s inner;
  struct sensor_data_s outer;
};

#define INVALID_HUMIDITY    (0xFF)
#define INVALID_TEMP        (-100)

extern volatile uint8_t g_humi;
extern volatile int8_t  g_temp;

#define NRF24L01_DATA_LEN   (4)
#define NRF24L01_ADDR_LEN   (5)
#define NRF24L01_CHANNEL    (2)
#define NRF24L01_M_TXADDR   (0xD7)
#define NRF24L01_M_RXADDR   (0xE7)
#define NRF24L01_C_TXADDR   (NRF24L01_M_RXADDR)
#define NRF24L01_C_RXADDR   (NRF24L01_M_TXADDR)

enum NRF24L01_CMD
{
  NRF24L01_NULL = 0,
  NRF24L01_SENSOR = 1,
};


#endif
